<template>
  <div class="exam">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="科目">
        <el-select v-model="subjectId" filterable clearable @clear="subjectIdClear" placeholder="请选择">
          <el-option
            v-for="item in subject"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="章节">
        <el-input v-model="section" placeholder="章节"></el-input>
      </el-form-item>
      <el-form-item label="题目内容">
        <el-input v-model="question" placeholder="题目内容"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getAnswerInfo" icon="el-icon-a-042" style="font-size: 20px;">查询</el-button>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="showAdd" icon="el-icon-a-07" style="font-size: 20px;">添加</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="pagination.records" border :row-class-name="tableRowClassName">
      <el-table-column fixed="left" prop="subjectName" label="科目" width="180"></el-table-column>
      <el-table-column prop="question" label="题目信息" width="490"></el-table-column>
      <el-table-column prop="section" label="所属章节" width="200"></el-table-column>
      <el-table-column prop="type" label="题目类型" width="200"></el-table-column>
      <el-table-column prop="score" label="试题分数" width="150"></el-table-column>
      <el-table-column prop="level" label="难度等级" width="133"></el-table-column>
      <el-table-column fixed="right" label="操作" width="220">
        <template slot-scope="scope">
          <el-button @click="toEdit(scope.row.type, scope.row.questionId)" type="primary" icon="el-icon-a-032">编辑</el-button>
          <el-button @click="deleteById(scope.row.questionId, scope.row.type)" type="danger" icon="el-icon-a-022">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagination.current"
      :page-sizes="[4, 8, 10, 20]"
      :page-size="pagination.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.total"
      class="page"
    ></el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pagination: {
        //分页后的考试信息
        current: 1, //当前页
        total: null, //记录条数
        size: 10 //每页条数

      },
      subject: {},
      section: "",
      subjectId: "",
      question: "",
    };
  },
  created() {
    this.getAnswerInfo();
  },
  methods: {
    toEdit(type, id) {
      // 前往编辑题目
      this.$router.push({path:'/editAnswerChildren',query: {type: type, questionId: id}})
    },
    getAnswerInfo(size, current) {
      // 初始化科目数据
      this.initSubjectData();

      //分页查询所有试卷信息
      if(typeof size === 'number' && !isNaN(size)) {
        this.pagination.size = size;
      }
      if(typeof current === 'number' && !isNaN(current)) {
        this.pagination.current = current;
      } else {
        this.pagination.current = 1;
      }
      var subjectId = this.subjectId;
      if(this.subjectId.trim() == "") {
        subjectId = "@";
      }
      var section = this.section;
      if(this.section.trim() == "") {
        section = "@";
      }
      var question = this.question;
      if(this.question.trim() == "") {
        question = "@";
      }

      // var subjectId = this.subjectId == null ? 0 : this.subjectId;

      this.$axios(
        `/api/answers/${this.pagination.current}/${this.pagination.size}/${subjectId}/${section}/${question}`
      )
        .then(res => {
          this.pagination = res.data.data;
        })
        .catch(error => {});
    },
    subjectIdClear() {
      this.form.subjectId = null
    },
    /**
     * 加载专业数据
     */
    initSubjectData() {
      this.subject = [];
      this.$axios({
        url: `/api/subjectFindAll`,
        method: 'get',
      }).then(res => {
        this.subject = res.data.data;
      })
    },
    deleteById(questionId, type) { //删除题目
      var _this = this;
      this.$confirm("确定删除当前题目吗？删除后无法恢复", "Warning", {
        confirmButtonText: '确定删除',
        cancelButtonText: '算了,留着吧',
        type: 'danger'
      }).then(() => { //确认删除
        this.$axios({
          url: `/api/answers/${questionId}/${type}`,
          method: 'delete',
        }).then(res => {
          if (res.data.status == 200) {
            this.$message({
              message: res.data.message,
              type: 'success'
            })
          }
          this.getAnswerInfo(this.pagination.size, this.pagination.current)
        })
      }).catch(() => {

      })
    },
    showAdd() { //增加题库
   // this.$router.push({path:'/editAnswerChildren',query: {type: type, questionId: id}})
      this.$router.push({path:'/addAnswerSubject'})
    },
    //改变当前记录条数
    handleSizeChange(val) {
      this.getAnswerInfo(val);
    },
    //改变当前页码，重新发送请求
    handleCurrentChange(val) {
      this.getAnswerInfo(undefined, val);
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 == 0) {
        return "warning-row";
      } else {
        return "success-row";
      }
    }
  }
};
</script>
<style lang="less" scoped>
.exam {
  padding: 0px 40px;
  .page {
    margin-top: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .edit {
    margin-left: 20px;
  }
  .el-table tr {
    background-color: #DD5862 !important;
  }
}
  .el-table .warning-row {
    background: #000 !important;

  }

  .el-table .success-row {
    background: #DD5862;
  }

</style>
